package com.ruoyi.pyo.wechat;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.pyo.domain.PetYeahWechatUser;
import com.ruoyi.pyo.service.IPetYeahWechatUserService;
import com.ruoyi.pyo.wechat.body.resq.LoginResqBody;
import com.ruoyi.pyo.wechat.tools.WeChatTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api("微信用户接口")
@RestController
@RequiredArgsConstructor
@ConditionalOnBean(WeChatTools.class)
@RequestMapping("/wechat/")
public class WechatController {
    private final AuthenticationManager authenticationManager;
    private final WeChatTools weChatTools;
    private final IPetYeahWechatUserService petYeahWechatUserService;
    private final TokenService tokenService;

    @PostMapping("jscode2session")
    @ApiOperation("jscode2session")
    public AjaxResult jscode2session(@RequestParam String jsCode) {
        return AjaxResult.success(weChatTools.jscode2session(jsCode));
    }


    @PostMapping("get-user-phone-number")
    @ApiOperation("获取用户手机号")
    public AjaxResult getUserPhoneNumber(@RequestParam String code) {
        return AjaxResult.success(weChatTools.getUserPhoneNumber(code));
    }


    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginResqBody loginBody)
    {
        //组装token信息
        LoginUser loginUser = new LoginUser();
        PetYeahWechatUser user;
        if (StringUtils.isNotBlank(loginBody.getUnionid())) {
            user = petYeahWechatUserService.getOne(Wrappers.lambdaQuery(PetYeahWechatUser.class).eq(PetYeahWechatUser::getUnionid, loginBody.getUnionid()));
            loginUser.setWxId(user.getUnionid());
        } else if (StringUtils.isNotBlank(loginBody.getOpenid())) {
            user = petYeahWechatUserService.getOne(Wrappers.lambdaQuery(PetYeahWechatUser.class).eq(PetYeahWechatUser::getOpenid, loginBody.getOpenid()));
            loginUser.setWxId(user.getOpenid());
        } else if (StringUtils.isNotBlank(loginBody.getPhoneNumber())) {
            user = petYeahWechatUserService.getOne(Wrappers.lambdaQuery(PetYeahWechatUser.class).eq(PetYeahWechatUser::getTelNo, loginBody.getPhoneNumber()));
            loginUser.setWxId(user.getTelNo());
        } else {
            throw new RuntimeException("微信openid unionid phoneNumber必须有一个存在");
        }
        PetYeahWechatUser wechatUser = new PetYeahWechatUser();
        wechatUser.setOpenid(loginBody.getOpenid());
        wechatUser.setTelNo(loginBody.getPhoneNumber());
        wechatUser.setUnionid(loginBody.getUnionid());
        if (user == null) {
            petYeahWechatUserService.save(wechatUser);
            user = wechatUser;
        }
        AjaxResult ajax = AjaxResult.success(user);
        ajax.put(Constants.TOKEN, tokenService.createToken(loginUser));
        // 生成token
        return ajax;
    }
}
